package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import bean.KechengBannerBean;
import jiyun.com.yiyanmeng.R;

import static com.umeng.commonsdk.internal.a.i;

public class KeChengAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private  List<KechengBannerBean.InfoBean.KechengBean>  list;

    public KeChengAdapter(Context context,  List<KechengBannerBean.InfoBean.KechengBean>  list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.kecheng_rv_item, null, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        MyHolder myHolder = (MyHolder) holder;
        //tv1
        myHolder.kecheng_rv_tv1.setText(list.get(position).getName());
        //tv2
        List<String> type_str = list.get(position).getType_str();
        String str = "";
        for (int i = 0; i < type_str.size(); i++) {
            str += type_str.get(i) + " ";
        }
        myHolder.kecheng_rv_tv2.setText(str);
        //tv3
        myHolder.kecheng_rv_tv3.setText(list.get(position).getClass_hour() + "课时");
        //img
        Glide.with(context).load(list.get(position).getPic()).into(myHolder.kecheng_rv_img);
        //接口回调
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private final ImageView kecheng_rv_img;
        private final TextView kecheng_rv_tv1;
        private final TextView kecheng_rv_tv2;
        private final TextView kecheng_rv_tv3;

        public MyHolder(View view) {
            super(view);
            kecheng_rv_img = view.findViewById(R.id.kecheng_rv_img);
            kecheng_rv_tv1 = view.findViewById(R.id.kecheng_rv_tv1);
            kecheng_rv_tv2 = view.findViewById(R.id.kecheng_rv_tv2);
            kecheng_rv_tv3 = view.findViewById(R.id.kecheng_rv_tv3);
        }
    }

    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}
